<?php
class BookManageController extends Controller{
	//展示所有图书/**

	public function indexAction(){
		$title="图书管理";
		$include_file="allbook";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$bookmodel=new BookModel("tb_bookinfo");
				//$allbook=$bookmodel->getAllBook();
		//定义每页数量
		$pagesize = 10;
		//接收页数参数,如果为空,则显示第一页
		$current = isset ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		//计算偏移量
		$offset = ($current - 1) * $pagesize;
		//获取总条数,条件where为空
		$total = $bookmodel->total("");
		$allbook = $bookmodel->getBooks($offset, $pagesize);
		//载入工具类,获取分页信息
		$this->library ( "Page" );
		$page=new Page($total, $pagesize, $current,"index.php",array("c"=>"bookmanage","a"=>"index"));
		$pageinfo=$page->showPage();
		include VIEWS_PATH."index.html";
	}
	
	//载入添加图书表单
	public function addAction(){
		$title="添加图书";
		$include_file="addbook";
		$libraryinfo=$this->getFooter();
		$nav=$this->getNavs();
		
		//获取全部图书类型/出版社/书架信息
		$bookmodel=new BookModel("tb_booktype");
		$booktypes=$bookmodel->getAllBookType();
		$publishings=$bookmodel->getAllPublishing();
		$bookcases=$bookmodel->getAllBookCase();
		include VIEWS_PATH."index.html";
	}
	
	//插入数据
	public function insertAction(){
		$data['barcode']=$_POST['barcode']+0;
		$data['bookname']=$_POST['bookname'];
		$data['typeid']=$_POST['typeid']+0;
		$data['author']=$_POST['author'];
		$data['translator']=$_POST['translator'];
		$data['ISBN']=$_POST['ISBN'];
		$data['price']=$_POST['price']+0;
		$data['page']=$_POST['page'];
		$data['bookcase']=$_POST['bookcase']+0;
		$data['storage']=$_POST['storage']+0;
		$data['inTime']=date("Y-m-d");
		$data['operator']=$_SESSION['admin_name'];
		if (strlen($data['barcode']) >= 9) {
			$this->jump("index.php?c=bookmanage&a=addbook","图书条形码最长9位数字!",3);
		}
		
		//判断是否为空
		if (empty($data['barcode'])||empty($data['bookname'])) {
			$this->jump("index.php?c=bookmanage&a=addbook","图书条形码和图书名不能为空!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$bookmodel=new BookModel("tb_bookinfo");
		if ($bookmodel->insert($data)) {
			$this->jump("index.php?c=bookmanage&a=index","添加成功!",3);
		}
	}
	
	//修改图书表单
	public function editAction(){
		$title="修改图书信息";
		$include_file="editbook";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		//获取全部图书类型/出版社/书架信息
		$bookmodels=new BookModel("tb_booktype");
		$booktypes=$bookmodels->getAllBookType();
		$publishings=$bookmodels->getAllPublishing();
		$bookcases=$bookmodels->getAllBookCase();
		$pk=$_GET['id']+0;
		$bookmodel=new BookModel("tb_bookinfo");
		$onebook=$bookmodel->selectByPk($pk);
		
		
		include VIEWS_PATH."index.html";
	}
	
	//完成图书更新
	public function updateAction(){
		$data['barcode']=$_POST['barcode'];
		$data['bookname']=$_POST['bookname'];
		$data['typeid']=$_POST['typeid'];
		$data['author']=$_POST['author'];
		$data['translator']=$_POST['translator'];
		$data['ISBN']=$_POST['ISBN'];
		$data['price']=$_POST['price'];
		$data['page']=$_POST['page'];
		$data['bookcase']=$_POST['bookcase'];
		$data['storage']=$_POST['storage'];
		$data['id']=$_POST['id'];
		$data['inTime']=date("Y-m-d");
		$data['operator']=$_SESSION['admin_name'];
		
		//判断是否为空
		if (empty($data['barcode'])||empty($data['bookname'])) {
			$this->jump("index.php?c=bookmanage&a=edit&id={$data['id']}","图书条形码和图书名不能修改为空!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$bookmodel=new BookModel("tb_bookinfo");
		if ($bookmodel->update($data)) {
			$this->jump("index.php?c=bookmanage&a=index","修改成功!",2);
		}else {
			$this->jump("index.php?c=bookmanage&a=edit&id={$data['id']}","修改失败!",3);
		}
	
		
	}
	
	//删除图书
	public function deleteAction(){
		$pk=$_GET['id']+0;
		$bookmodel=new BookModel("tb_bookinfo");
		if ($bookmodel->delete($pk)) {
			$this->jump("index.php?c=bookmanage&a=index", "删除成功!",2);
		}else {
			$this->jump("index.php?c=bookmanage&a=index", "删除失败,请刷新页面后重试!",3);
		}
	}
}